package map.route

import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.route.*
import map.MapBaseManager
import map.default.DefaultGetRoutePlanResultListener
import map.ext.locationManager

class RouteManager : MapBaseManager() {

    private var routePlanSearch: RoutePlanSearch? = null

    private var routeCall: DefaultGetRoutePlanResultListener? = null

    override fun onCreate() {
        routePlanSearch = RoutePlanSearch.newInstance().apply {
            setOnGetRoutePlanResultListener(object : OnGetRoutePlanResultListener {
                override fun onGetWalkingRouteResult(p0: WalkingRouteResult?) {
                    routeCall?.onGetWalkingRouteResult(p0)
                }

                override fun onGetTransitRouteResult(p0: TransitRouteResult?) {
                    routeCall?.onGetTransitRouteResult(p0)
                }

                override fun onGetMassTransitRouteResult(p0: MassTransitRouteResult?) {
                    routeCall?.onGetMassTransitRouteResult(p0)
                }

                override fun onGetDrivingRouteResult(p0: DrivingRouteResult?) {
                    routeCall?.onGetDrivingRouteResult(p0)
                }

                override fun onGetIndoorRouteResult(p0: IndoorRouteResult?) {
                    routeCall?.onGetIndoorRouteResult(p0)
                }

                override fun onGetBikingRouteResult(p0: BikingRouteResult?) {
                    routeCall?.onGetBikingRouteResult(p0)
                }
            })
        }
    }

    fun walkPlan(
        start: LatLng = locationManager().locationLatLng(),
        end: LatLng,
        call: DefaultGetRoutePlanResultListener
    ) {
        routeCall = call
        routePlanSearch?.walkingSearch(WalkingRoutePlanOption().apply {
            this.mFrom = PlanNode.withLocation(start)
            this.mTo = PlanNode.withLocation(end)
        })
    }

    fun ridePlan(
        start: LatLng = locationManager().locationLatLng(),
        end: LatLng,
        call: DefaultGetRoutePlanResultListener
    ) {
        routeCall = call
        routePlanSearch?.bikingSearch(BikingRoutePlanOption().apply {
            this.mFrom = PlanNode.withLocation(start)
            this.mTo = PlanNode.withLocation(end)
        })
    }


    override fun onDestroy() {
        routePlanSearch?.destroy()
        routePlanSearch = null
    }
}